﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.IO;
using System.Windows.Forms;

namespace weiqijiance.转速仪
{
    public class zsymq : zsybass
    {
        /// <summary>
        /// 打开串口
        /// </summary>
        /// <param name="sSet1">串口设置</param>
        /// <returns>是否成功打开</returns>
        public override bool openCOM(string sSet1)
        {
            bool breturn = base.openCOM(sSet1);
            commex.ReadTimeout = 500;
            commex.ReceivedBytesThreshold = 5;//达到50字节以后才开始触发串口
            commex.DataReceived += comGAS_DataReceived;
            return breturn;
        }

        public override bool SendDeviceCmd()
        {
            return true;
        }
        void comGAS_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (commex.IsOpen)
            {
                System.Threading.Thread.Sleep(100);
                int n = commex.BytesToRead;//
                byte[] buf1 = new byte[n];//声明一个临时数组存储当前来的串口数据  
                byte[] buf = new byte[n];
                commex.Read(buf1, 0, n);
                string scom = "";
                scom = System.Text.Encoding.Default.GetString(buf1);
                string sscom = "";
                sscom = scom;
                string[] hexValuesSplit = sscom.Split('r');
                int icount = 0;
                icount = hexValuesSplit.Length;
                if (icount > 0)
                {
                    //   int.TryParse(hexValuesSplit[icount-1].Substring(0, hexValuesSplit[icount-1].Length-1), out m_iTurn);
                    if (sscom != "")
                    {
                        if (sscom.Length > 4)
                        {
                            int.TryParse(sscom.Substring(0, 4), out m_iGasturn);

                        }
                    }

                }
            }
        }
    }
}
